package org.sy.lab.Java7并发编程实战.一线程管理.九线程局部变量的使用;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SafeTask implements Runnable {
	private static ThreadLocal<Date> startDate = new ThreadLocal<Date>() {
		protected Date initialValue() {
			return new Date();
		}
	};
	@Override
	public void run() {
		int time = (int)Math.rint(Math.random()*10);
		System.out.printf("Starting Thread: %s: %s 等待%d秒\n", Thread.currentThread().getId(), startDate.get(), time);
		try {
			TimeUnit.SECONDS.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.printf("Thread Finished: %s: %s\n", Thread.currentThread().getId(), startDate.get());
	}
}
